package com.codeczx.completeuidemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchPopupActivity extends AppCompatActivity {

    RVAdapter adapter;
    PopupWindow popupWindow;
    List<String> strs;
    List<String> tmpStrs;
    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.ll_et)
    LinearLayout llEt;
    @BindView(R.id.et2)
    EditText et2;
    @BindView(R.id.rv2)
    RecyclerView rv2;
    private int ITEM_HEIGHT = 55;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_popup);
        ButterKnife.bind(this);

        final View popupView = getLayoutInflater().inflate(R.layout.popupwindow, null);
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);

        et.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!popupWindow.isShowing()) {
                    popupWindow.showAsDropDown(et);
                    if (et.getText().length() != 0) {
                        tmpStrs.clear();
                        for (int i = 0; i < strs.size(); i++) {
                            if (strs.get(i).contains(et.getText())) {
                                tmpStrs.add(strs.get(i));
                            }
                        }
                        adapter.updateData(tmpStrs);
                        int size = tmpStrs.size() > 5 ? 5 : tmpStrs.size();
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(600, ITEM_HEIGHT * size);
                        rv.setLayoutParams(lp);
                    } else {
                        adapter.updateData(strs);
                        int size = strs.size() > 5 ? 5 : strs.size();
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(600, ITEM_HEIGHT * size);
                        rv.setLayoutParams(lp);
                    }
                }
            }
        });

        strs = new ArrayList<>();
        tmpStrs = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            strs.add("我是第" + i + "个item");
        }

        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() != 0) {
                    tmpStrs.clear();
                    for (int i = 0; i < strs.size(); i++) {
                        if (strs.get(i).contains(s)) {
                            tmpStrs.add(strs.get(i));
                        }
                    }
                    adapter.updateData(tmpStrs);
                    int size = tmpStrs.size() > 5 ? 5 : tmpStrs.size();
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(600, ITEM_HEIGHT * size);
                    rv.setLayoutParams(lp);
                } else {
                    adapter.updateData(strs);
                    int size = strs.size() > 5 ? 5 : strs.size();
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(600, ITEM_HEIGHT * size);
                    rv.setLayoutParams(lp);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (!popupWindow.isShowing()) {
                        popupWindow.showAsDropDown(et);
                        if (et.getText().length() != 0) {
                            tmpStrs.clear();
                            for (int i = 0; i < strs.size(); i++) {
                                if (strs.get(i).contains(et.getText())) {
                                    tmpStrs.add(strs.get(i));
                                }
                            }
                            adapter.updateData(tmpStrs);
                            int size = tmpStrs.size() > 5 ? 5 : tmpStrs.size();
                            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(600, ITEM_HEIGHT * size);
                            rv.setLayoutParams(lp);
                        } else {
                            adapter.updateData(strs);
                            int size = strs.size() > 5 ? 5 : strs.size();
                            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(600, ITEM_HEIGHT * size);
                            rv.setLayoutParams(lp);
                        }
                    }
                }
            }
        });

        adapter = new RVAdapter(strs);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        boolean isTouchEditText = false;
        if (x >= et.getLeft() && x <= et.getRight() && y >= et.getTop() && y <= et.getBottom()) {
            isTouchEditText = true;
        }
        if (popupWindow != null && popupWindow.isShowing() && !isTouchEditText) {
            popupWindow.dismiss();
        }
        return super.onTouchEvent(event);
    }

    @OnClick({R.id.et, R.id.rv, R.id.et2, R.id.rv2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.et:
                break;
            case R.id.rv:
                break;
            case R.id.et2:
                break;
            case R.id.rv2:
                break;
        }
    }
}
